<?php
namespace app\api\controller\soft;
use app\providers\BaseController;
use auth\Auths;
use app\api\service\User as UserService;
use hg\apidoc\annotation as Apidoc;
/**
 * @Apidoc\Title("系统登录")
 * @Apidoc\Group("base")
 * @Apidoc\Sort(1)
 */
class Auth extends BaseController
{
 
    /*
     * 构造函数
     */
    public function __construct()
    {
        $this->service = new UserService();
    }
	
   	/**
     * 账密登录
     * @Apidoc\Author("weifashi")
	 * @Apidoc\Url("/api/soft/auth/pwdLogin")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("usercode",require=true,default="admin",desc="账号")
     * @Apidoc\Param("pwd", require=true, type="string", default="1",desc="密码" )
     * @Apidoc\After(event="setGlobalHeader",key="x-token",value="res.data.data.access_token",desc="我的全局Header参数")
     * @global login false
    */
    public function pwdLogin($usercode,$pwd){
		if(!$usercode || !$pwd){
			return \Api::ReError("请求参数错误！");
		}
		$info = $this->service->pwdLogin($usercode,$pwd);
    	return \Api::ReSucess($info);
    }

    /**
     * 刷新token
     * @Apidoc\Author("weifashi")
	 * @Apidoc\Url("/api/soft/auth/refreshToken")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("refresh_token", require=true, default="",desc="refresh_token" )
     * @global login false
     */
	public function refreshToken(){
		$data = array("code"=>-10,"data"=>"","msg"=>"");
		$refresh_token = request()->post("refresh_token");
	 	if( empty($refresh_token) ){
			$data['msg'] = '未找到refresh_token！';
			return  json($data);
	 	}
		if( !$userInfo = Auths::instance('api')->refreshToken($refresh_token) ){
			$data['msg'] = 'token过期，请重新登录！';
			return  json($data);
		}
		return json(array("code"=>0,"data"=>$userInfo,"msg"=>"token成功刷新!"));
	} 
	
	/**
    * 退出
	*@Apidoc\Url("/api/soft/auth/logout")
	* @global login false
    */
    public function logout()
    {
    	$token = request()->header('x-token');
       	Auths::instance('api')->logout($token);
        return json(array("code"=>0,"data"=>"","msg"=>"退出成功"));
    }
	
}
